import {request} from '@/utils/request'
import type { 
  HbtWorkflowStart, 
  HbtWorkflowApprove, 
  HbtWorkflowReject,
  HbtWorkflowTransit,
  HbtConditionEvaluate,
  HbtConditionEvaluateResult,
  HbtWorkflowTodoQuery,
  HbtWorkflowDoneQuery,
  HbtWorkflowTask,
  HbtInstanceStatus, 
  HbtTransition, 
  HbtNode,
  HbtTransitionQuery,
  HbtTransitionPagedResult
} from '@/types/workflow/engine'
import type { HbtInstanceTrans } from '@/types/workflow/trans'
import type { HbtInstanceOper } from '@/types/workflow/oper'
import type { HbtPagedResult } from '@/types/common'

// 启动工作流实例
export function startWorkflow(data: HbtWorkflowStart) {
  return request<number>({
    url: '/api/HbtEngine/start',
    method: 'post',
    data
  })
}

/**
 * 暂停工作流
 */
export function suspendWorkflow(instanceId: string, reason: string = '手动暂停') {
  return request<boolean>({
    url: `/api/HbtWorkflowEngine/${instanceId}/suspend`,
    method: 'post',
    data: { reason }
  })
}

/**
 * 恢复工作流
 */
export function resumeWorkflow(instanceId: string) {
  return request<boolean>({
    url: `/api/HbtWorkflowEngine/${instanceId}/resume`,
    method: 'post'
  })
}

/**
 * 终止工作流
 */
export function terminateWorkflow(instanceId: string, reason: string) {
  return request<boolean>({
    url: `/api/HbtWorkflowEngine/${instanceId}/terminate`,
    method: 'post',
    data: { reason }
  })
}

// 审批工作流
export function approveWorkflow(data: HbtWorkflowApprove) {
  return request<boolean>({
    url: '/api/HbtEngine/approve',
    method: 'post',
    data
  })
}

/**
 * 获取工作流状态
 */
export function getWorkflowStatus(instanceId: string) {
  return request<HbtInstanceStatus>({
    url: `/api/HbtWorkflowEngine/${instanceId}/status`,
    method: 'get'
  })
}

/**
 * 获取可用转换
 */
export function getAvailableTransitions(instanceId: string) {
  return request<HbtTransition[]>({
    url: `/api/HbtWorkflowEngine/${instanceId}/transitions`,
    method: 'get'
  })
}

/**
 * 获取当前节点
 */
export function getCurrentNode(instanceId: string) {
  return request<HbtNode>({
    url: `/api/HbtWorkflowEngine/${instanceId}/current-node`,
    method: 'get'
  })
}

/**
 * 获取工作流变量
 */
export function getWorkflowVariables(instanceId: string) {
  return request<Record<string, any>>({
    url: `/api/HbtWorkflowEngine/${instanceId}/variables`,
    method: 'get'
  })
}

/**
 * 设置工作流变量
 */
export function setWorkflowVariables(instanceId: string, variables: Record<string, any>) {
  return request<boolean>({
    url: `/api/HbtWorkflowEngine/${instanceId}/variables`,
    method: 'put',
    data: variables
  })
}

/**
 * 获取工作流历史
 */
export function getWorkflowHistory(instanceId: string) {
  return request<HbtInstanceTrans[]>({
    url: `/api/HbtWorkflowEngine/${instanceId}/history`,
    method: 'get'
  })
}

/**
 * 获取工作流操作
 */
export function getWorkflowOperations(instanceId: string) {
  return request<HbtInstanceOper[]>({
    url: `/api/HbtWorkflowEngine/${instanceId}/operations`,
    method: 'get'
  })
}

// 转换历史相关API

// 获取转换历史列表
export function getTransitionList(params: HbtTransitionQuery) {
  return request<HbtPagedResult<HbtTransition>>({
    url: '/api/HbtEngine/transition/list',
    method: 'get',
    params
  })
}

// 获取转换历史详情
export function getTransition(transitionId: string) {
  return request<HbtTransition>({
    url: `/api/HbtEngine/transition/${transitionId}`,
    method: 'get'
  })
}

// 拒绝工作流
export function rejectWorkflow(data: HbtWorkflowReject) {
  return request<boolean>({
    url: '/api/HbtEngine/reject',
    method: 'post',
    data
  })
}

// 手动触发节点转换
export function transitWorkflow(data: HbtWorkflowTransit) {
  return request<boolean>({
    url: '/api/HbtEngine/transit',
    method: 'post',
    data
  })
}

// 评估条件表达式
export function evaluateCondition(data: HbtConditionEvaluate) {
  return request<HbtConditionEvaluateResult>({
    url: '/api/HbtEngine/evaluate-condition',
    method: 'post',
    data
  })
}

// 获取待办任务列表
export function getTodoTasks(userId: string, query: HbtWorkflowTodoQuery) {
  return request<HbtWorkflowTask[]>({
    url: `/api/HbtEngine/todo/${userId}`,
    method: 'get',
    params: query
  })
}

// 获取已办任务列表
export function getDoneTasks(userId: string, query: HbtWorkflowDoneQuery) {
  return request<HbtWorkflowTask[]>({
    url: `/api/HbtEngine/done/${userId}`,
    method: 'get',
    params: query
  })
} 